package com.gaoyi.basemodule.baseUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.gaoyi.basemodule.baseControl.MyRoundedCorners;

import java.util.Date;

public class ImageUtil {

    private static RequestOptions requestOptions;

    public static void loadImg(Context context, ImageView iv, String url) {
        Glide.with(context)
                .load(url)
                .skipMemoryCache(false)
                .into(iv);
    }

    public static void loadImg(Context context, ImageView iv, String url, int radius) {
        Glide.with(context)
                .load(url)
                .skipMemoryCache(false)
                .apply(RequestOptions.bitmapTransform(new MyRoundedCorners(DensityUtil.dp2px(radius))))
                .into(iv);
    }

    public static void loadBitmap(Context context, ImageView iv, Bitmap bitmap, int radius) {
        Glide.with(context)
                .load(bitmap)
                .skipMemoryCache(false)
                .apply(RequestOptions.bitmapTransform(new MyRoundedCorners(DensityUtil.dp2px(radius))))
                .into(iv);
    }

    public static void loadImg(Context context, ImageView iv, String url, int error, int radius) {
        Glide.with(context)
                .load(url)
                .error(error)
                .skipMemoryCache(false)
                .apply(RequestOptions.bitmapTransform(new MyRoundedCorners(DensityUtil.dp2px(radius))))
                .into(iv);
    }

    public static void loadImg(ImageView iv, String url, int error, int radius) {
        Glide.with(iv)
                .load(url)
                .error(error)
                .skipMemoryCache(false)
                .apply(RequestOptions.bitmapTransform(new MyRoundedCorners(DensityUtil.dp2px(radius))))
                .into(iv);
    }

    public static void loadImg(ImageView iv, String url, int error) {
        if (requestOptions == null) {
            requestOptions = new RequestOptions()
                    .skipMemoryCache(false)
                    .priority(Priority.HIGH)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }
        Glide.with(iv)
                .load(url)
                .error(error)
                .apply(requestOptions)
                .skipMemoryCache(false)
                .into(iv);
    }

    public static void loadGifImg(Context context, ImageView iv, int url) {
        Glide.with(context)
                .load(url)
                .skipMemoryCache(false)
                .into(iv);
    }

    /**
     * 保存本地图片的名称
     */
    public static String getImgName() {
        return "img_" + DateFormatUtil.DateTimeToStr(new Date(), "yyyyMMddHHmmss") + ".jpg";
    }

}
